home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / UUENCODE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  2.7 KB  |  119 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if !defined(_lint)
  19. static char rcsid[] OPTIONAL = "$Id: uuencode.c,v 1.7 1997/07/31 00:44:20 root Exp root $";
  20. #endif
  21.  
  22. /*
  23.  * uuencode [input] output
  24.  *
  25.  * Encode a file so it can be mailed to a remote system.
  26.  */
  27. #include <stdio.h>
  28. #ifndef _lint
  29. #include <stdlib.h>
  30. #endif
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33.  
  34. #ifdef NOPE
  35. /* ENC is the basic 1 character encoding function to make a char printing */
  36. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  37. #else
  38. /* ENC is the basic 1 character encoding function to make a char printing */
  39. #define ENC(c) (((c) & 077) + ' ')
  40. #endif
  41.  
  42. main(argc, argv)
  43. char **argv;
  44. {
  45.     FILE *in;
  46.     struct stat sbuf;
  47.     int mode;
  48.  
  49.     /* optional 1st argument */
  50.     if (argc > 2) {
  51.         if ((in = fopen(argv[1], "rb")) == NULL) {
  52.             perror(argv[1]);
  53.             exit(1);
  54.         }
  55.         argv++; argc--;
  56.     } else
  57.         in = stdin;
  58.  
  59.     if (argc != 2) {
  60.         fprintf(stderr,"Usage: uuencode [infile] remotefile\n");
  61.         exit(2);
  62.     }
  63.  
  64.     /* figure out the input file mode */
  65.     if (fstat(fileno(in), &sbuf) < 0 || !isatty(fileno(in)))
  66.         mode = 0666 & ~umask(0666);
  67.     else
  68.         mode = sbuf.st_mode & 0777;
  69.     printf("begin %o %s\n", mode, argv[1]);
  70.  
  71.     encode(in, stdout);
  72.  
  73.     printf("end\n");
  74.     exit(0);
  75. }
  76.  
  77. /*
  78.  * copy from in to out, encoding as you go along.
  79.  */
  80. encode(in, out)
  81. register FILE *in;
  82. register FILE *out;
  83. {
  84.     char buf[80];
  85.     register int i, n;
  86.  
  87.     for (;;) {
  88.         /* 1 (up to) 45 character line */
  89.         n = fread(buf, 1, 45, in);
  90.         putc(ENC(n), out);
  91.  
  92.         for (i=0; i<n; i += 3)
  93.             outdec(&buf[i], out);
  94.  
  95.         putc('\n', out);
  96.         if (n <= 0)
  97.             break;
  98.     }
  99. }
  100.  
  101. /*
  102.  * output one group of 3 bytes, pointed at by p, on file f.
  103.  */
  104. outdec(p, f)
  105. register char *p;
  106. register FILE *f;
  107. {
  108.     register int c1, c2, c3, c4;
  109.  
  110.     c1 = *p >> 2;
  111.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  112.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  113.     c4 = p[2] & 077;
  114.     putc(ENC(c1), f);
  115.     putc(ENC(c2), f);
  116.     putc(ENC(c3), f);
  117.     putc(ENC(c4), f);
  118. }
  119.